/*
photoeffect.js v1.8 for ppblog by martin $ 2009/04/18 00:15:11
Copyright (c) 2007-2009, martin. http://p2b.jp
Usage:
 main-className: photo-effect
  sub-className: photo-frame (default)
                 stamp-frame
                 photo-angle[from -360 to 360]
  e.g. <img src="foo.jpg" alt="photo-effect" class="photo-effect photo-angle5" />
  ** Recommended `photo-angle` is -5 to 5.
*/
if(document.namespaces){
 try{
  document.namespaces.add("v","urn:schemas-microsoft-com:vml","#default#VML");
 } catch(e){;}
}

var photoEffect={
 imgs : [],
 done : false,
 init : function(ob){
  var d = document, nodes=[], item; ob = ob || d;
  photoEffect.imgs = [];
  if(d.querySelectorAll){
   var items = ob.querySelectorAll("img.photo-effect"), index = items.length;
   while(index) nodes[--index] = items[index];
  } else {
   try {
    var xp = d.evaluate('.//img[contains(concat(" ", @class, " "), " photo-effect ")]', ob, null, 0, null);
    for(item = xp.iterateNext(); item; item = xp.iterateNext()) nodes.push(item);
   } catch(e){
    var node, cls, items = ob.getElementsByTagName("IMG");
    for(var i = 0, l = items.length; i < l; i++){
     item = items[i];
     if(item.className){
      cls = item.className.split(/\s+/);
      for(var j = 0, k = cls.length; j < k; j++){
       if(cls[j] == "photo-effect"){
        nodes[nodes.length] = item; break;
       }
      }
     }
    }
   }
  }
  if(nodes.length){
   if(!photoEffect.photoframe){
    photoEffect.photoframe = new Image();
    photoEffect.photoframe.src = "Images/photoframe.png";
    photoEffect.stampframe = new Image();
    photoEffect.stampframe.src = "Images/stampframe.png";
   }
   for(var i = 0, l = nodes.length; i < l; i++){
    node = nodes[i];
    try { photoEffect.imgs.push(node);} catch(e) {;}
   }
  } else return;
  if(photoEffect.stampframe.width > 50 && photoEffect.photoframe.width > 50) photoEffect.fire();
  photoEffect.photoframe.onload = photoEffect.stampframe.onload = function(){
   if(photoEffect.stampframe.complete && photoEffect.photoframe.complete){
    photoEffect.fire();
   } else setTimeout(arguments.callee, 1);
  }
 },
 fire : function(){
  var d = document, img, iw, ih, rot, arot, ar, ab, bx, by, fw, fh, w, h, canvas, cntxt, frame;
  for(var i = 0, len = photoEffect.imgs.length; i < len; i++){
   var img = photoEffect.imgs[i];
   img.onload = function(){
    var K = this;
    if(K.photoEffected) return;
    iw = K.width; ih = K.height; rot = K.className.match(/photo-angle(\-?\d+)/);
    rot = rot ? Math.PI * rot[1]/180 : 0;
    arot = Math.abs(rot);
    frame = d.namespaces ? d.createElement("v:*") : new Image();
    if(K.className.indexOf("stamp-frame")!=-1){
     ar=1.02;ab=0.85;frame.src=photoEffect.stampframe.src;
    } else {
     ar=1.07;ab=0.7;frame.src=photoEffect.photoframe.src;
    }
    bx=parseInt(Math.sqrt(iw)*ab);
    by=parseInt(Math.sqrt(ih)*ab);
    fw=iw+2*bx;fh=Math.round((ih+by)*ar)+by;
    w=parseInt(fw*Math.cos(arot)+fh*Math.sin(arot));
    h=parseInt(fw*Math.sin(arot)+fh*Math.cos(arot));
    if(d.namespaces){
     var dx = dy = 0;
     canvas = d.createElement('<v:* style="width:'+w+"px; height:"+h+'px;">');
     canvas.setAttribute("id", "photo_canvas_K"+i);
     canvas.innerHTML = "";
     if(rot>0){
      dx = fh * Math.sin(rot); dy = 0;
     }
     if(rot<0){
      dx = 0; dy = fw * Math.sin(arot);
     }d.body.appendChild(canvas);
     canvas.innerHTML = '<?xml:namespace prefix="v" /><v:group style="position: relative; display: block;width:'+w+"px; height:"+h+"px; rotation:"+(180*rot/Math.PI)+';" coordsize="'+w+","+h+'"><v:rect filled="f" stroked="f" style="position:absolute; left:'+dx+"px; top:"+dy+"px; width:"+fw+"px; height:"+fh+'px;"><v:imagedata src="'+frame.src+'" /></v:rect><v:rect stroked="f" style="position:absolute; padding:0; left:'+(bx+dx)+"px; top:"+(by+dy)+"px; width:"+iw+"px; height:"+ih+'px;"><v:imagedata src="'+K.src+'" /></v:rect></v:group>';
     canvas.style.cssText = K.style.cssText;
     canvas.title = K.title;
     
     var _canvas = canvas.cloneNode();
     _canvas.innerHTML = canvas.innerHTML;
     K.parentNode.replaceChild(_canvas, K);
     canvas.style.display = "none"; canvas = _canvas;
    } else {
     canvas=d.createElement("canvas");
     canvas.setAttribute("id","photo_canvas_K"+i);
     d.body.appendChild(canvas);
     canvas.style.cssText=K.style.cssText;canvas.title=canvas._title=(typeof K._title!="undefined")?K._title:K.title;canvas.height=h;canvas.style.height=h+"px";canvas.width=w;canvas.style.width=w+"px";cntxt=canvas.getContext("2d");if(rot>0){cntxt.translate(fh*Math.sin(rot),0)}if(rot<0){cntxt.translate(0,fw*Math.sin(arot))}if(rot!=0){cntxt.rotate(rot)}try{cntxt.drawImage(frame,0,0,fw,fh);cntxt.drawImage(K,bx,by,iw,ih);K.parentNode.replaceChild(canvas,K)}catch(e){}
    }
    canvas.className = K.className;
    if(canvas.parentNode.parentNode){
     canvas.parentNode.parentNode.className += " photo-canvas";
    }
    if(K.getAttribute("onclick")!=""){
     canvas.setAttribute("onclick",K.getAttribute("onclick"));
    }
    K.photoEffected = true;
    if(i + 1 == len){ photoEffect.done = true;}
   };//img.onload
   if(img.complete && !img.photoEffected){
    img.onload();
   }
  };//#for-loop
 }
};